<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ include file="../../common/tag.jsp"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>用户登陆成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

<%@ include file="../../common/header.jsp"%>
<link href="${ctx }/css/plugins/bootstrap-table/bootstrap-table.min.css"
	rel="stylesheet">
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>礼券管理</h5>
					</div>
					<div class="ibox-content">
						<input type="hidden" name="mpId" id="mpId" />

						<div class="input-group">
							<button class="btn btn-primary" type="button"
								style="margin-bottom: 0px;" onclick="addGiftConfig();">
								<i class="glyphicon glyphicon-plus"></i>&nbsp;新增礼券
							</button>
						</div>
					</div>
				</div>
			</div>
		</div>

		<!-- Panel Other -->
		<div class="ibox float-e-margins" id="tableShow">
			<div class="ibox-title">
				<h5>新人礼奖品</h5>
			</div>
			<div class="ibox-content">
				<div class="row row-lg">
					<div class="col-sm-12">
						<!-- Example Events -->
						<div class="example-wrap">
							<table id="newCustGiftListTable" data-mobile-responsive="true"
								data-click-to-select="true" data-unique-id="id">
								<thead>
								</thead>
							</table>
						</div>
					</div>
					<!-- End Example Events -->
				</div>
			</div>
		</div>

		<!-- Panel Other -->
		<div class="ibox float-e-margins" id="tableShow">
			<div class="ibox-title">
				<h5>感恩礼奖品</h5>
			</div>
			<div class="ibox-content">
				<div class="row row-lg">
					<div class="col-sm-12">
						<!-- Example Events -->
						<div class="example-wrap">
							<table id="thankfulGiftListTable" data-mobile-responsive="true"
								data-click-to-select="true" data-unique-id="id">
								<thead>
								</thead>
							</table>
						</div>
					</div>
					<!-- End Example Events -->
				</div>
			</div>
		</div>


	</div>

	<div style="" class="modal fade" id="myModal">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModallabel">消息提示</h4>
				</div>
				<div class="modal-body">
					是否删除<span id="deletecontent"></span>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-primary" id="confirm"
						onclick="confirm()">确定删除</button>
					<button type="button" class="btn btn-default" data-dismiss="modal"
						id="cancel" onclick="cancel()">取消删除</button>

				</div>
			</div>
		</div>
	</div>
	
	
	<script src="js/jquery.min.js?v=2.1.4"></script>
	<script src="js/bootstrap.min.js?v=3.3.6"></script>
	<script src="js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
	<script src="js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
	<script
		src="js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
	<!--时间选择-->
	<script src="js/plugins/layer/laydate/laydate.js"></script>
	<!--下拉框-->
	<script src="js/plugins/chosen/chosen.jquery.js"></script>

	<script>
    function addGiftConfig(){
    	window.location.href="<%=basePath%>giftCertificate/jumpGiftConfigAddOrUpdate"; 
    }
    var $newCustGiftTable = $('#newCustGiftListTable');
    var $thankfulGiftListTable = $('#thankfulGiftListTable');

    var search = function () {
        $newCustGiftTable.bootstrapTable('refresh');
        $thankfulGiftListTable.bootstrapTable('refresh');
    }
    var config = {
        ".chosen-select": {},
        ".chosen-select-deselect": {allow_single_deselect: !0},
        ".chosen-select-no-single": {disable_search_threshold: 10},
        ".chosen-select-no-results": {no_results_text: "Oops, nothing found!"},
        ".chosen-select-width": {width: "95%"}
    };

    $(document).ready(function () {

    	//将表单元素和table属性转成json
        queryParams = function (params) {
            var temp = {};
            temp.currentResult = params.offset;
            temp.showCount = params.limit;
            /* var formParam = $('#queryForm').serializeArray();
            for (var i = 0; i < formParam.length; i++) {
                if (formParam[i].value != '') {
                    temp[formParam[i].name] = formParam[i].value;
                }
            } */
            return temp;
        };
        
        rowStyle = function (row, index) {
            var classes = ['active', 'success', 'info', 'warning', 'danger'];
            if (index % 2 === 0 && index / 2 < classes.length) {
                return {
                    classes: classes[index / 2]
                };
            }
            return {};
        }

        $newCustGiftTable.bootstrapTable({
            url: "giftCertificate/queryNewCustGiftList",
            pagination: !0,
            showRefresh: !0,
            showToggle: !0,
            showColumns: !0,
            iconSize: "outline",
            striped: true, //是否显示行间隔色
            queryParams: queryParams,//传递参数（*）
            sidePagination: "server", //服务端处理分页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [5, 10, 20, 50],        //可供选择的每页的行数（*）
            minimunCountColumns: 2,      //可供选择的每页的行数（*）
            //detailView: true,
            sortName: undefined,
            sortOrder: 'asc',
            sortStable: false,
            type: "post",
            contentType: "application/json; charset=utf-8",
            rowStyle: rowStyle,
            iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list",
                detailOpen: 'glyphicon-plus icon-plus',
                detailClose: 'glyphicon-minus icon-minus',
                sortable: 'glyphicon-minus icon-minus'
            },
            onLoadSuccess: function (data) {
                if (data.total < 5) {
                    $newCustGiftTable.bootstrapTable('getOptions')["height"] = 400;
                } else {
                    $newCustGiftTable.bootstrapTable('getOptions')["height"] = '';
                }

                $newCustGiftTable.bootstrapTable('hideColumn', 'id');
                $newCustGiftTable.bootstrapTable('hideColumn', 'edit');
            },
            columns: [
                {
                    title: '奖品图片',
                    field: 'picUrl',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                    	if(value== null || value == '') {
                        	 value = ctx+"/img/logo/logo.png";	
                    	}
                    	return "<img src='"+value+"' style='width:50px' />";
                    }
                },
                {
                    title: '奖品名称',
                    field: 'name',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '奖品类型',
                    field: 'prizeType',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                    	if(row.prizeType == "coupon") {
                    		return "门店券";
                    	} else if(row.prizeType == "point") {
                    		return "积分";
                    	} else if(row.prizeType == "product") {
                    		return "实物";
                    	} 
                    }
                },
                {
                    title: '数量(剩余库存/总库存)',
                    field: 'sku',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                   		return (parseInt(row.totalQty)-parseInt(row.usedQty))+"/"+row.totalQty;
                    }
                },
                {
                    title: '概率',
                    field: 'probability',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                   		return row.probability+"%";
                    }
                },
                {
                    title: '操作',
                    field: 'operate',
                    align: 'center',
                    width : '140',
                    valign: 'middle',
                    formatter: function (value, row, index) {
                          return '<a><button class="btn  btn-xs" id="editButton" onclick="editGiftConfigDetail(\''+row.id+'\','+index+')" title="修改"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button></a>'+
                                '&nbsp;<a><button class="btn  btn-xs" onclick="delete_tr(this,\''+row.id+'\')" title="删除"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></a>';

                    }
                }
            ]
        });
        
        $thankfulGiftListTable.bootstrapTable({
            url: "giftCertificate/queryThankfulGiftList",
            pagination: !0,
            showRefresh: !0,
            showToggle: !0,
            showColumns: !0,
            iconSize: "outline",
            striped: true, //是否显示行间隔色
            queryParams: queryParams,//传递参数（*）
            sidePagination: "server", //服务端处理分页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [5, 10, 20, 50],        //可供选择的每页的行数（*）
            minimunCountColumns: 2,      //可供选择的每页的行数（*）
            //detailView: true,
            sortName: undefined,
            sortOrder: 'asc',
            sortStable: false,
            type: "post",
            contentType: "application/json; charset=utf-8",
            rowStyle: rowStyle,
            iconsPrefix: 'glyphicon', // glyphicon of fa (font awesome)
            icons: {
                refresh: "glyphicon-repeat",
                toggle: "glyphicon-list-alt",
                columns: "glyphicon-list",
                detailOpen: 'glyphicon-plus icon-plus',
                detailClose: 'glyphicon-minus icon-minus',
                sortable: 'glyphicon-minus icon-minus'
            },
            onLoadSuccess: function (data) {
                if (data.total < 5) {
                    $thankfulGiftListTable.bootstrapTable('getOptions')["height"] = 400;
                } else {
                    $thankfulGiftListTable.bootstrapTable('getOptions')["height"] = '';
                }

                $thankfulGiftListTable.bootstrapTable('hideColumn', 'id');
                $thankfulGiftListTable.bootstrapTable('hideColumn', 'edit');
            },
            columns: [
                {
                    title: '奖品图片',
                    field: 'picUrl',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                    	if(value== null || value == '') {
                       		value = ctx+"/img/logo/logo.png";	
	                   	}
	                   	return "<img src='"+value+"' style='width:50px' />";
                    }
                },
                {
                    title: '奖品名称',
                    field: 'name',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '奖品类型',
                    field: 'prizeType',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                    	if(row.prizeType == "coupon") {
                    		return "门店券";
                    	} else if(row.prizeType == "point") {
                    		return "积分";
                    	} else if(row.prizeType == "product") {
                    		return "实物";
                    	} 
                    }
                },
                {
                    title: '数量(剩余库存/总库存)',
                    field: 'sku',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                   		return (parseInt(row.totalQty)-parseInt(row.usedQty))+"/"+row.totalQty;
                    }
                },
                {
                    title: '概率',
                    field: 'probability',
                    align: 'center',
                    valign: 'middle',
                    formatter:function(value,row,index) {
                   		return row.probability+"%";
                    }
                },
                {
                    title: '操作',
                    field: 'operate',
                    align: 'center',
                    width : '140',
                    valign: 'middle',
                    formatter: function (value, row, index) {
                          return '<a><button class="btn  btn-xs" id="editButton" onclick="editGiftConfigDetail(\''+row.id+'\','+index+')" title="修改"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></button></a>'+
                                '&nbsp;<a><button class="btn  btn-xs" onclick="delete_tr(this,\''+row.id+'\')" title="删除"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button></a>';

                    }
                }
            ]
        });
        for (var selector in config)$(selector).chosen(config[selector]);
    });

    function tableHeight() {
        return $(window).height() - 50;
    }
    var delete_tr_id = null;
    function delete_tr(a) {
        var s = $(a).parent().parent().parent().children().eq(1).text();
        delete_tr_id = $(a).parent().parent().parent().attr("data-uniqueid");
        $("#deletecontent").html(s);
        $("#tableShow").hide();
        $("#myModal").modal('show');
    }
    function cancel() {
        $("#tableShow").show();
        $("#myModal").modal('hide');
    }
    function confirm() {
        $.ajax({
        	url: "<%=basePath%>giftCertificate/deleteGiftConfig",
            type: "post",
            data: {"id": delete_tr_id},
            dataType: "json",
            success: function (note) {
                $("#tableShow").show();
                $("#myModal").modal('hide');
                $newCustGiftTable.bootstrapTable('refresh');
                $thankfulGiftListTable.bootstrapTable('refresh');
            },
            error: function () {
                alert("删除异常，请稍后重试");
            }

        });
    }
    //修改数据
    function editGiftConfigDetail(id, index){
		window.location.href = '<%=basePath%>giftCertificate/jumpGiftConfigAddOrUpdate?id='
					+ id;
		}
	</script>
</body>
</html>